package com.dongdongshop.Service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.data.Page;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.vo.SpecVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@org.springframework.stereotype.Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;

    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public List<TbSpecification> getSpList() {
        return tbSpecificationMapper.selectByExample(null);
    }

    @Override
    public Page<TbSpecification> spPage(String specName,Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<TbSpecification> list = tbSpecificationMapper.spPage(specName);
        PageInfo<TbSpecification> page = new PageInfo<>(list);
        return new Page<TbSpecification>(page.getPageNum(),page.getPageSize(),page.getTotal(),page.getPages(),page.getList());
    }

    @Override
    public Boolean addModal(TbSpecification tbSpecification, String[] optionName, Integer[] orders) {
            //增加返回主键id
            Integer sid = tbSpecificationMapper.insertGetId(tbSpecification);
            List<Map<String, Object>> list = new ArrayList<>();
            for (int i = 0; i < optionName.length; i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("optionName",optionName[i]);
                map.put("orders",orders[i]);
                list.add(map);
            }
            Long id = tbSpecification.getId();
            Integer i =tbSpecificationOptionMapper.insertBatchs(id,list);

            return i > 0;

    }


    //批量删除
    @Override
    public Boolean deleteInfo(Long[] id) {
        try{
            //删除品牌表
            Boolean b = tbSpecificationMapper.deleteInfo(id);
            //循环遍历数组删除每条数据对应表
            for (int i = 0;i < id.length;i++){
                TbSpecificationOptionExample example = new TbSpecificationOptionExample();
                TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
                criteria.andSpecIdEqualTo(id[i]);
                tbSpecificationOptionMapper.deleteByExample(example);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }

    }

    //删除
    @Override
    public Boolean deleteById(Long id) {
        try{
            //删除品牌表
            tbSpecificationMapper.deleteByPrimaryKey(id);
            //删除对应表
            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(id);
            tbSpecificationOptionMapper.deleteByExample(example);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    //回显
    @Override
    public SpecVO getSpecById(Long id) {
        //创建vo对象
        SpecVO vo = new SpecVO();
        //根据id查询
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        vo.setSpec(tbSpecification);
        //再根据id擦查询数据
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> options = tbSpecificationOptionMapper.selectByExample(example);
        vo.setOptionList(options);
        return vo;
    }


    //修改
    @Override
    public Boolean updateModal(TbSpecification tbSpecification, String[] optionName, Integer[] orders) {
        Integer t = tbSpecificationMapper.updateGetId(tbSpecification);
        //删除对应表
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(tbSpecification.getId());
        tbSpecificationOptionMapper.deleteByExample(example);
        //修改对应表
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < optionName.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("optionName",optionName[i]);
            map.put("orders",orders[i]);
            list.add(map);
        }
        Long id = tbSpecification.getId();
        Integer i = tbSpecificationOptionMapper.insertBatchs(id,list);
        return i > 0;
    }
}
